/**
 * Created by wangshaohua on 2018/1/13.
 */
moduleContent.controller("ContentTaskQuery", function ($stateParams, ExerciseContentResource, LanguageService, $scope, $log, $state, GeneralResource, LoadingService, ConstantService, DialogService) {
    initPage($scope);
    $scope.$on("$viewContentLoaded", function () {
        $scope.user = ConstantService.getItem('user');
        $scope.reset();
        pageOnLoad($scope);
        ConstantService.addState($state.current, true, LanguageService.language('任务查询'));
        $scope.currentParentId;
        $scope.recordBar = {};
        $scope.query(1);
    });

    $scope.reset = function () {
        $scope.queryPara = {
            key: null,
            space: Enums.ExerciseContentSpaceEnum.TASK
        };
    }

    $scope.onQueryKeyDown = function () {
        if (event.keyCode == 13) {
            $scope.query(1);
        }
    }

    $scope.query = function (pageNo, direction) {
        $scope.recordBar.bar = false;
        if (!direction) {
            direction = 'DOWN';
        }
        $scope.queryPara.parentId = $scope.currentParentId;
        ExerciseContentResource.query({pageNo: pageNo - 1, pageSize: 10, direction: direction}, $scope.queryPara, function (queryResult) {
            $log.debug("查询结果", queryResult.data);
            $scope.queryResult = queryResult.data.list;
            $scope.currentParentId = queryResult.data.parentId;
            $scope.pagination.setPage(queryResult.data.list, pageNo);
        });
    };
    $scope.pagination = new Pagination();
    $scope.pagination.setPageClickFn($scope.query);

    $scope.openDetail = function (record) {
        $state.go("sz.contentTaskDetail", {
            contentId: record.id
        });
    }

    $scope.openFolder = function (record) {
        $scope.currentParentId = record.id;
        $scope.query(1);
    }

    $scope.addContent = function () {
        DialogService.open("addContentTaskGroup", $scope, function () {
            if ($scope.contentId) {
                $state.go("sz.contentTaskDetail", {
                    contentId: $scope.contentId
                });
            }
        });
    }

    $scope.addFolder = function () {
        $scope.currentFolder = {
            space: Enums.ExerciseContentSpaceEnum.TASK,
            parentId: $scope.currentParentId
        }
        DialogService.open("contentFolderMgt", $scope, function () {
            $scope.query(1);
        });
    }

    /**
     *
     */
    $scope.backSpace = function () {
        $scope.query(1,'UP');
    }

    $scope.changeCheck = function () {
        $scope.recordBar.bar = false;
        $scope.recordBar.showEdit = false;
        $scope.recordBar.showMove = false;

        var checkedLength = 0;
        for (var i = 0; i < $scope.queryResult.datas.length; i++) {
            if ($scope.queryResult.datas[i].checked) {
                if ($scope.queryResult.datas[i].recordType == Enums.ExerciseContentRecordTypeEnum.FOLDER) {
                    $scope.recordBar.showEdit = true;
                }
                checkedLength++;
            }
        }
        if (checkedLength > 0) {
            if (checkedLength > 1) {
                $scope.recordBar.showEdit = false;
            }
            $scope.recordBar.showMove = true;
        }

        if (!$scope.recordBar.showEdit && !$scope.recordBar.showMove) {
            $scope.recordBar.bar = false;
        } else {
            $scope.recordBar.bar = true;
        }
    }

    $scope.edit = function () {
        for (var i = 0; i < $scope.queryResult.datas.length; i++) {
            if ($scope.queryResult.datas[i].checked && $scope.queryResult.datas[i].recordType == Enums.ExerciseContentRecordTypeEnum.FOLDER) {
                $scope.currentFolder = $scope.queryResult.datas[i];
                DialogService.open("contentFolderMgt", $scope, function () {
                    $scope.query(1);
                });
                return;
            }
        }
    }

    $scope.move = function () {
        $scope.currentSpace = Enums.ExerciseContentSpaceEnum.QUIZ;
        $scope.currentRecordType = Enums.ExerciseContentRecordTypeEnum.FOLDER;
        $scope.selectedContentIds = new Array();

        for (var i = 0; i < $scope.queryResult.datas.length; i++) {
            if ($scope.queryResult.datas[i].checked) {
                $scope.selectedContentIds.push($scope.queryResult.datas[i].id);
            }
        }

        DialogService.open("exerciseContentMove", $scope, function () {
            $scope.query(1);
        });
    }
});